package com.ringtheweb.common.util;

/**
 * Utility class for {@link Class} management.
 * @author ldewavri
 *
 */
public class ClassUtils {

	/**
	 * Return the default classloader
	 * 
	 * @return the default classloader (never <code>null</code>)
	 * @see java.lang.Thread#getContextClassLoader()
	 */
	public static ClassLoader getDefaultClassLoader() {
		ClassLoader cl = null;
		try {
			cl = Thread.currentThread().getContextClassLoader();
		} catch (Throwable ex) {
		}
		if (cl == null) {
			/*
			 * if no context classloader,
			 *  return the one that loaded the current class 
			 */
			cl = ClassUtils.class.getClassLoader();
		}
		return cl;
	}
	
	/**
	 * Return the ClassLoader for the given class
	 * 
	 * @return the default classloader (never <code>null</code>)
	 * @see java.lang.Thread#getContextClassLoader()
	 */
	public static ClassLoader getClassLoader(Class<?> clazz) {
		ClassLoader cl = null;
		try {
			cl = Thread.currentThread().getContextClassLoader();
		} catch (Throwable ex) {
		}
		if (cl == null) {
			/*
			 * if no context classloader,
			 *  return the one that loaded the current class 
			 */
			cl = clazz.getClassLoader();
		}
		return cl;
	}
	


}
